package cc.openkit.kitadmin_2.api.controller.admin;

import cc.openkit.common.KitUtil;
import cc.openkit.kitadmin_2.entity.admin.KitAdmin;
import cc.openkit.kitadmin_2.service.admin.KitAdminService;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class KitAdminController {
    @Autowired
    KitAdminService kitAdminService;

    /**
     * 登录接口
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Object getCallId(String username, String password, HttpSession session) {

        Map<String, Object> map = new HashMap<String, Object>();

        KitAdmin kitAdmin = kitAdminService.queryKitAdminByUsername(username);
        if(kitAdmin == null){
            // 保存前台传来的缩略图
            return KitUtil.returnMap("10001","用户名不存在");
        }

        if(!kitAdmin.getPassword().equals(password)){
            // 保存前台传来的缩略图
            return KitUtil.returnMap("10001","用户名密码不匹配");
        }
        session.setAttribute("loginUser", kitAdmin);
        // 保存前台传来的缩略图
        map.put("code", 20000);
        map.put("msg", "登录成功");
        kitAdmin.setPassword("");
        map.put("kitAdmin", kitAdmin);
        return JSONObject.toJSON(map);
    }
}
